package com.htx.zqs.blesmartmask.broadcastreceiver;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;

import com.clj.fastble.BleManager;
import com.htx.zqs.blesmartmask.MyApplication;
import com.htx.zqs.blesmartmask.R;
import com.htx.zqs.blesmartmask.eventbus.MessageEvent;
import com.htx.zqs.blesmartmask.utils.Utils;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by zhao on 2018/5/30.
 */

public class BleBroadcastReceiver extends BroadcastReceiver {
    private Activity activity;

    public BleBroadcastReceiver(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        switch (action) {
            case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                if (activity != null && BleManager.getInstance().getAllConnectedDevice().size() == 0) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(activity)
                            .setTitle(activity.getResources().getString(R.string.blebroadcast_title))
                            .setMessage(activity.getResources().getString(R.string.blebroadcast_message))
                            .setPositiveButton(activity.getResources().getString(R.string.ok), null);

                    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            activity.finish();
                            EventBus.getDefault().post(new MessageEvent("1", 555));
                        }
                    });
                    builder.show();
                }
                MyApplication.isConnected = false;
                break;
        }

    }

}
